package com.wh1200.mybatis.xmlless.model

import com.wh1200.mybatis.xmlless.ElementInfoBuilder
import com.wh1200.mybatis.xmlless.JavaClassWrapper
import com.wh1200.mybatis.xmlless.config.BaseResolverTest
import com.wh1200.mybatis.xmlless.enums.Operations
import com.wh1200.mybatis.xmlless.model.criteria.CriteriaParamName
import com.wh1200.mybatis.xmlless.model.dto.DefaultFieldWrapper
import com.wh1200.mybatis.xmlless.resolver.MappingResolver
import com.wh1200.mybatis.xmlless.resolver.ParameterResolver
import com.wh1200.mybatis.xmlless.util.TableInfoProvider
import com.wh1200.xmlless.test.dao.StudentDAO
import com.wh1200.xmlless.test.form.StudentQueryForm
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.javaMethod

/**
 * Created by 吴昊 on 2023/12/9.
 */
class QueryCriteriaTest : BaseResolverTest(
    StudentDAO::class.java
) {

  private val criteria = QueryCriteria(
      "name",
      Operations.Like,
      listOf(
          ParameterResolver.buildCriteriaParameter(
              CriteriaParamName("form.name", "name"),
              ElementInfoBuilder.build(
                  DefaultFieldWrapper(StudentQueryForm::name.javaField!!)
              )
          )
      ),
      createMethodInfo(StudentDAO::find.javaMethod!!),
      null,
      ConditionConnector.AND
  )

  @Test
  fun getColumns() {
    assertEquals(1, criteria.getColumns().size)
    assertEquals("t_student.name AS name", criteria.getColumns()[0].toSql())
  }

  @Test
  fun toSql() {
    assertEquals(
        """<if test="form.name != null and form.name.length() &gt; 0">
      |t_student.name LIKE CONCAT('%', #{form.name},'%') AND
      |</if>""".trimMargin(), trimLine(criteria.toSql())
    )
  }

  @Test
  fun toSqlWithoutTest() {
    assertEquals("t_student.name LIKE CONCAT('%', #{form.name},'%') AND", criteria.toSqlWithoutTest())
  }

  @Test
  fun getTests() {
    val c = QueryCriteria(
        "name",
        Operations.Like,
        listOf(
            ParameterResolver.buildCriteriaParameter(
                CriteriaParamName("form.name", "name"),
                ElementInfoBuilder.build(
                    DefaultFieldWrapper(StudentQueryForm::name.javaField!!)
                )
            )
        ),
        createMethodInfo(StudentDAO::find.javaMethod!!),
        null,
        ConditionConnector.AND
    )
    assertEquals("form.name != null and form.name.length() &gt; 0", c.getTests(null))
  }

  @Test
  fun getTests2() {
    MappingResolver.resolve(TableInfoProvider.getOrInitXmllessTableInfo(JavaClassWrapper(modelClass)))
    val field = StudentQueryForm::type.javaField!!
    val c = QueryCriteria(
        "type",
        Operations.Like,
        listOf(
            ParameterResolver.buildCriteriaParameter(
                CriteriaParamName("form.type", "type"),
                ElementInfoBuilder.build(DefaultFieldWrapper(field))
            )
        ),
        createMethodInfo(StudentDAO::find.javaMethod!!),
        null,
        ConditionConnector.AND
    )
    val detailInfo = ElementInfoBuilder.build(DefaultFieldWrapper(field))
    assertEquals(
        "form.type == 5",
        c.getTests(
            TestInfo("== 5", detailInfo)
        )
    )
  }

}
